package cn.itcast.oa.action;

import java.util.List;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.oa.base.BaseAction;
import cn.itcast.oa.base.ModelDrivenBaseAction;
import cn.itcast.oa.model.Department;
import cn.itcast.oa.util.DepartmentUtils;
import com.opensymphony.xwork2.ActionContext;

@SuppressWarnings("serial")
@Controller
@Scope("prototype")
public class DepartmentAction extends ModelDrivenBaseAction<Department>{
	
	//放在action的变量会放在值栈里，可以直接取出用
	private Long parentId;

	//添加部门方法
     public String add() throws Exception{
    	 if(parentId==null){
    	    model.setParent(null);
    	 }else{
    		 model.setParent(departmentService.getById(parentId));
    	 }
  
    	 departmentService.save(model);
    	 return "tolist";
     }
     
     public String delete() throws Exception{
//    	 System.out.println(parentId+"----------------------");
    	 departmentService.delete(model.getId());
    	 return "tolist";
     }
     
     public String list() throws Exception{
    	List<Department> departmentList=null;
    	
    	 if(parentId == null){
    		 //如果没有上级部门的话就显示所有的顶级部门
    		 departmentList=departmentService.findTopList();
    	 }else{
    		 //否则就根据顶级部门显示子部门
    		 departmentList=departmentService.getChildren(parentId);
    		 //显示上级部门
    		 Department parent=departmentService.getById(parentId);
    		 //把parent放在map中
    		 ActionContext.getContext().put("parent", parent);
    	 }
    	 //List<Department> departmentList=departmentService.findAll();
    	 ActionContext.getContext().put("departmentList", departmentList);
    	 
    	 return "list";
     }
     
     public String addUI() throws Exception{
    	 
    	 List<Department> topList=departmentService.findTopList();
    	 List<Department> departmentList=DepartmentUtils.getAllDepartments(topList);
    	 ActionContext.getContext().put("departmentList",departmentList);
    	 return "addUI";
     }
     
     public String editUI() throws Exception{
    	 List<Department> topList=departmentService.findTopList();
    	 List<Department> departmentList=DepartmentUtils.getAllDepartments(topList);
    	 ActionContext.getContext().put("departmentList", departmentList);
    	 
    	 //准备回显信息
    	 Department department=departmentService.getById(model.getId()); 
    	 ActionContext.getContext().getValueStack().push(department);
    	 
    	 if (department.getParent() != null) {
 			parentId = department.getParent().getId();
 		}
    	 return "editUI";
     }
     
     public String edit() throws Exception{
    	 Department department=departmentService.getById(model.getId());
         department.setName(model.getName());
         if(parentId==null){
        	 department.setParent(null);
         }
         department.setParent(departmentService.getById(parentId));
         department.setDescription(model.getDescription());
         departmentService.update(department);
    	 return "tolist";
     }
//===============
	public Long getParentId() {
		return parentId;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}

	
}
